﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Data.Entity;
using Service;

namespace ECommerce.Administration.Controls
{
	public partial class ShowOnHomePageProdManage : AdminBaseUsercontrol
	{
		private Service.IProductService _ProductService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (!IsPostBack)
			{
				LabelProductName.Text = GetLocalResourceString("Admin.EditShowOnHomepage.ProductNameLabel.Text");
				CheckRecommend.Text = GetLocalResourceString("Admin.EditShowOnHomepage.CheckRecommend.Text");
				CheckShowOnHome.Text = GetLocalResourceString("Admin.EditShowOnHomepage.CheckShowOnHome.Text");
				ButtonSearch.Text = GetLocalResourceString("Admin.SearchButton.Text");
			}
		}

		protected void ButtonSearch_Click(object sender, EventArgs e)
		{
		}

		protected void GridProducts_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.ProductList.NameHeader");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.ProductList.ListPriceHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.ProductList.CreatedOnHeader");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.ProductList.UpdatedOnHeader");
				e.Row.Cells[4].Text = GetLocalResourceString("Admin.ProductList.QuantityHeader");
				e.Row.Cells[5].Text = GetLocalResourceString("Admin.ProductList.RecommendHeader");
				e.Row.Cells[6].Text = GetLocalResourceString("Admin.ProductList.ShowOnHomePageHeader");
				e.Row.Cells[7].Text = GetLocalResourceString("Admin.ProductList.DisplayOrderHeader");
			}

			if(e.Row.DataItem != null)
			{
				var CheckRecommend = e.Row.FindControl("CheckRecommend") as CheckBox;
				var CheckShowOnHome = e.Row.FindControl("CheckShowOnHome") as CheckBox;

				var data = e.Row.DataItem as Product;

				CheckRecommend.Checked = (data.Recommend.HasValue ? data.Recommend.Value : false);
				CheckShowOnHome.Checked = data.ShowOnHomePage;
			}
		}
		
		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();
		}

		private void BindData()
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				if (_ProductService == null)
				{
					_ProductService = new ProductService();
				}

				var productByLang = _ProductService.GetAllProductsByLanguage(SiteContext.Current.WorkingLanguage.LanguageId);

				if (productByLang != null)
				{
					var data = productByLang.Where(p => p.Published);

					var textName = TextProdName.Text.Trim();
					if (!string.IsNullOrEmpty(textName))
					{
						if (data != null)
						{
							data = data.Where(exp => exp.Name.Contains(textName));
						}
					}

					if (CheckRecommend.Checked)
					{
						if (data != null)
						{
							data = data.Where(exp => exp.Recommend == true);
						}
					}

					if (CheckShowOnHome.Checked)
					{
						if (data != null)
						{
							data = data.Where(exp => exp.ShowOnHomePage == true);
						}
					}

					GridProducts.DataSource = data;
					GridProducts.DataBind();
				}
				else
				{
					GridProducts.DataSource = null;
					GridProducts.DataBind();
				}
			}
		}

		public void UpdateProducts()
		{
			foreach(GridViewRow row in GridProducts.Rows)
			{
				HiddenField hfProductID = row.FindControl("hfProductID") as HiddenField;
				var CheckRecommend = row.FindControl("CheckRecommend") as CheckBox;
				var CheckShowOnHome = row.FindControl("CheckShowOnHome") as CheckBox;
				int theProductID = int.Parse(hfProductID.Value);
				var TextDisplayOrder = row.FindControl("TextDisplayOrder") as TextBox;

				if (_ProductService == null)
				{
					_ProductService = new ProductService();
				}

				var theProduct = _ProductService.GetProduct(theProductID);

				var displayOrder = 0;
				if (int.TryParse(TextDisplayOrder.Text.Trim(), out displayOrder))
				{
					theProduct.DisplayOrder = displayOrder;
				}
				theProduct.Recommend = CheckRecommend.Checked;
				theProduct.ShowOnHomePage = CheckShowOnHome.Checked;
				
				_ProductService.UpdateProduct(theProduct);
			}
		}
	}
}